package cn.edu.ctbu.dp07.controller;

import cn.edu.ctbu.dp07.entity.Course;
import cn.edu.ctbu.dp07.entity.Score;
import cn.edu.ctbu.dp07.entity.Student;
import cn.edu.ctbu.dp07.service.CourseService;
import cn.edu.ctbu.dp07.service.ScoreService;
import cn.edu.ctbu.dp07.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    private ScoreService scoreService;
    
    @Autowired
    private StudentService studentService;
    
    @Autowired
    private CourseService courseService;

    /**
     * 显示成绩列表页面
     */
    @GetMapping("/list")
    public String list(Model model) {
        List<Score> scores = scoreService.findAll();
        model.addAttribute("scores", scores);
        return "score/list";
    }

    /**
     * 显示添加成绩页面
     */
    @GetMapping("/add")
    public String add(Model model) {
        model.addAttribute("score", new Score());
        model.addAttribute("students", studentService.findAll());
        model.addAttribute("courses", courseService.findAll());
        return "score/edit";
    }

    /**
     * 显示编辑成绩页面
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable Integer id, Model model) {
        Score score = scoreService.findById(id);
        model.addAttribute("score", score);
        model.addAttribute("students", studentService.findAll());
        model.addAttribute("courses", courseService.findAll());
        return "score/edit";
    }

    /**
     * 保存成绩信息
     */
    @PostMapping("/save")
    public String save(@RequestParam(value = "id", required = false) Integer id,
                      @RequestParam("studentId") Integer studentId,
                      @RequestParam("courseId") Integer courseId,
                      @RequestParam("score") Double score,
                      @RequestParam("semester") String semester,
                      @RequestParam(value = "examTime", required = false) String examTime,
                      @RequestParam(value = "remark", required = false) String remark) {
        
        Score scoreEntity;
        if (id != null) {
            scoreEntity = scoreService.findById(id);
        } else {
            scoreEntity = new Score();
        }
        
        scoreEntity.setStudentId(studentId);
        scoreEntity.setCourseId(courseId);
        scoreEntity.setScore(score);
        scoreEntity.setSemester(semester);
        scoreEntity.setExamTime(examTime);
        scoreEntity.setRemark(remark);
        
        scoreService.save(scoreEntity);
        return "redirect:/score/list";
    }

    /**
     * 删除成绩
     */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        scoreService.deleteById(id);
        return "redirect:/score/list";
    }
    
    /**
     * 查看学生成绩
     */
    @GetMapping("/student/{studentId}")
    public String studentScores(@PathVariable Integer studentId, Model model) {
        Student student = studentService.findById(studentId);
        List<Score> scores = scoreService.findByStudent(student);
        
        model.addAttribute("student", student);
        model.addAttribute("scores", scores);
        model.addAttribute("averageScore", scoreService.getAverageScoreByStudent(student));
        
        return "score/student-scores";
    }
    
    /**
     * 查看课程成绩
     */
    @GetMapping("/course/{courseId}")
    public String courseScores(@PathVariable Integer courseId, Model model) {
        Course course = courseService.findById(courseId);
        List<Score> scores = scoreService.findByCourse(course);
        
        model.addAttribute("course", course);
        model.addAttribute("scores", scores);
        model.addAttribute("averageScore", scoreService.getAverageScoreByCourse(course));
        
        return "score/course-scores";
    }
} 